﻿using SmartWMS.Common.Extensions;
using SmartWMS.Common.ResponseOutput;
using SmartWMS.Domain.BasicsDatum;
using SmartWMS.Domain.StatementManage;
using SmartWMS.Domain.WarehouseManage;
using SmartWMS.Server.FinanceManage.PayTypeServices.Dto;
using SmartWMS.Server.Repository;
using SmartWMS.Server.StatementServer.InventoryServices.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace SmartWMS.Server.StatementServer.InventoryServices
{
    public class InventoryService : IInventoryService
    {
        private readonly IRepository<Inventory> _inventoryRepository;
        private readonly IRepository<Warehouse> _warehouseRepository;
        private readonly IRepository<Location> _locationRepository;
        private readonly IRepository<InWarehouseDetail> _inWarehouseDetailRepository;

        public InventoryService(IRepository<Inventory> inventoryRepository, IRepository<Warehouse> warehouseRepository, IRepository<Location> locationRepository, IRepository<InWarehouseDetail> inWarehouseDetailRepository)
        {
            _inventoryRepository = inventoryRepository;
            _warehouseRepository = warehouseRepository;
            _locationRepository = locationRepository;
            _inWarehouseDetailRepository = inWarehouseDetailRepository;
        }

        /// <summary>
        /// 批次下拉框
        /// </summary>
        /// <returns></returns>
        public async Task<Response<List<InWarehouseDetail>>> GetBatchList()
        {
            var batchList= await _inWarehouseDetailRepository.GetAllListAsync();
            var response = batchList.MapToList<InWarehouseDetail>();
            return response != null ? Response<List<InWarehouseDetail>>.Ok(response) : Response<List<InWarehouseDetail>>.Fail("查询失败");
        }

        /// <summary>
        /// 库存清单列表(分页)
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<Response<List<InventoryDto>>> GetInventoryDtoPes(InventorySelect dto)
        {
            var inventory = _inventoryRepository.GetListQuery();
            var warehouse = _warehouseRepository.GetListQuery();
            var location = _locationRepository.GetListQuery();
            var inWarehouseDetail = _inWarehouseDetailRepository.GetListQuery();
            var list = (from a in inventory
                        join b in warehouse on a.WarehouseId equals b.Id
                        join c in location on a.LocationId equals c.Id
                        join d in inWarehouseDetail on a.ProductCode equals d.ProductCode
                        select new InventoryDto
                        {
                            Id = a.Id,
                            ProductCode = a.ProductCode,
                            ProductName = a.ProductName,
                            ProductSpecification = a.ProductSpecification,
                            Sort = a.Sort,
                            Unit = a.Unit,
                            WarehouseName = b.Name,
                            LocationName = c.LocationName,
                            Batch = d.Batch
                        }).ToList();
            if (!string.IsNullOrEmpty(dto.code))
            {
                list = list.Where(x => x.ProductCode.Contains(dto.code)).ToList();
            }
            if (!string.IsNullOrEmpty(dto.name))
            {
                list = list.Where(x => x.ProductName.Contains(dto.name)).ToList();
            }
            if (!string.IsNullOrEmpty(dto.Warehouse))
            {
                list = list.Where(x => x.WarehouseName == dto.Warehouse).ToList();
            }
            if (!string.IsNullOrEmpty(dto.Batch))
            {
                list = list.Where(x => x.Batch == dto.Batch).ToList();
            }
            int totalCount = list.Count();
            int pageCount = (int)Math.Ceiling(totalCount / (double)dto.pageSize);
            list = list.Skip((dto.pageIndex - 1) * dto.pageSize).Take(dto.pageSize).ToList();
            return Response<List<InventoryDto>>.Ok(list, totalCount);
        }

        /// <summary>
        /// 仓库列表
        /// </summary>
        /// <returns></returns>
        public async Task<Response<List<Warehouse>>> GetWarehouseList()
        {
            var warehouseList = await _warehouseRepository.GetAllListAsync();
            var response = warehouseList.MapToList<Warehouse>();
            return response != null ? Response<List<Warehouse>>.Ok(response) : Response<List<Warehouse>>.Fail("查询失败");
        }
    }
}
